package com.ericsson.common.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 常规校验工具类
 *
 * @author yanghai
 *
 */
public class ValidateUtil {
	/**
	 * 校验数据是否为空
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {

		if (null == obj) {
			return true;
		}

		if (obj instanceof String) {
			String temp = (String) obj;
			if ("".equals(temp)) {
				return true;
			}
		}

		if (obj instanceof List) {
			@SuppressWarnings("rawtypes")
			List temp = (List) obj;
			if (temp.size() <= 0) {
				return true;
			}
		}

		if (obj instanceof Map) {
			@SuppressWarnings("rawtypes")
			Map temp = (Map) obj;
			if (temp.size() <= 0) {
				return true;
			}
		}

		if (obj instanceof Set) {
			@SuppressWarnings("rawtypes")
			Set temp = (Set) obj;
			if (temp.size() <= 0) {
				return true;
			}
		}

		if (obj instanceof Object[]) {
			Object[] temp = (Object[]) obj;
			if (temp.length <= 0) {
				return true;
			}
		}

		if (obj instanceof StringBuilder) {

			StringBuilder stringBuilder = (StringBuilder) obj;
			if (null == stringBuilder || stringBuilder.capacity() == 0
					|| stringBuilder.length() == 0) {
				return true;
			}
		}

		if (obj instanceof StringBuffer) {

			StringBuffer stringBuffer = (StringBuffer) obj;
			if (null == stringBuffer || stringBuffer.capacity() == 0
					|| stringBuffer.length() == 0) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 验证非空性
	 * 	可以判断字符串空值，集合元素为空的情况，且包含Null
	 *
	 * @param obj 要验证的对象
	 * @return 结果
	 */
	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}
}
